package model;

//general utility classes
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
//file handlers
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
//xml handlers
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;


/**
 * AcolyteGameType
 * defines the parameter for the card game we are playing
 **/
public class AcolyteGameType
{
  //turns have phases, players control zones ex. deck, hand, etc.
  private List<String> phases, zones;
  //private boolean stack;
  
  public AcolyteGameType(File typeFile)
  {
    phases = new LinkedList<String>();
    zones = new LinkedList<String>();
    try
    {
      FileInputStream in = new FileInputStream(typeFile);
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
      setup(reader);
    }
    catch(FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch(XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
  
  public AcolyteGameType(String filename)
  {
    phases = new LinkedList<String>();
    zones = new LinkedList<String>();
    try
    {
      FileInputStream in = new FileInputStream("gameTypes/" + filename);
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
      setup(reader);
    }
    catch(FileNotFoundException e)
    {
      e.printStackTrace();
    }
    catch(XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
  
  public void setup(XMLStreamReader reader) throws XMLStreamException
  {
    phases = new LinkedList<String>();
    zones = new LinkedList<String>();
    while("phases" != reader.getLocalName()) reader.nextTag();
    while(reader.hasNext())
    {
      reader.nextTag();
      if(reader.isStartElement())
      {
        if("phase" != reader.getLocalName())
          continue;//skip this tag if it isn't a phase tag
        phases.add(reader.getAttributeValue(null, "name"));
        
      }//we are done reading the phases
      else if("phase" == reader.getLocalName())
      {
        break;
      }
    }
    while("zones" != reader.getLocalName()) reader.nextTag();
    while(reader.hasNext())
    {
      reader.nextTag();
      if(reader.isStartElement())
      {
        if("zone" != reader.getLocalName())
          continue;//skip this tag if it isn't a phase tag
        zones.add(reader.getAttributeValue(null, "name"));
        
      }//we are done reading the deck
      else if("zone" == reader.getLocalName())
      {
        break;
      }
    }
  }
  
  public Iterator getPhases()
  {
    return phases.iterator();
  }
  
  public List<String> getZones()
  {
    return zones;
  }
}